﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.Mail;
using System.Configuration;
using System.Web.Configuration;
using System.Net.Configuration;
namespace gamexpert
{
    public partial class ContactUs : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (User.Identity.IsAuthenticated)
            {
                MembershipUser u = Membership.GetUser(User.Identity.Name);
                email.Text = u.Email;
                //email.Attributes.Add("onfocus", "this.blur()");
            }
            MultiView1.ActiveViewIndex = 0;
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            DataSet1 ds =new DataSet1();
            DataSet1TableAdapters.my_enquiryTableAdapter da=new DataSet1TableAdapters.my_enquiryTableAdapter();
            //Guid id = Guid.NewGuid();
            //Guid userid=new Guid();
            string username = "";
            if (User.Identity.IsAuthenticated==true)
            {
            //    MembershipUser u = Membership.GetUser(User.Identity.Name);
            //    userid = (Guid)u.ProviderUserKey;
                username = User.Identity.Name;
            }
            ds.my_enquiry.Addmy_enquiryRow(firstname.Text,lastname.Text,email.Text,enquiry.Text,DateTime.Now,username);
            try
            {
                da.Update(ds);
            }
            catch(Exception ex)
            {
            }

            //SEND MAIL
            Configuration configurationFile = WebConfigurationManager.OpenWebConfiguration("/gamexpert");
            MailSettingsSectionGroup mailSettings = (MailSettingsSectionGroup)configurationFile.GetSectionGroup("system.net/mailSettings");
            MailMessage msgMail = new MailMessage();
            string[] users=Roles.GetUsersInRole("Administrator");
            string emailaddresses = "";
            foreach (string s in users)
            {
                try
                {
                    MembershipUser u2 = Membership.GetUser(s);
                    emailaddresses += u2.Email + ";";
                }catch(Exception ex){
                }
            }
            msgMail.To = emailaddresses;
            msgMail.From = mailSettings.Smtp.From;
            msgMail.Subject = "[Notification]GameXpert Enquiry Form";
            msgMail.BodyFormat = MailFormat.Text;
            msgMail.Body = String.Format("Dear Admin,\n\nYou received enquiry.\n\nFirst Name:{0}\nLast Name:{1},\nEmail:{2}\nEnquiry:{3}\n\nRegards,\n{4}",firstname.Text,lastname.Text,email.Text,enquiry.Text,firstname.Text+" "+lastname.Text);
            //msgMail.Attachments.Add(new MailAttachment("c:\\temp\\annual-report.pdf"));
            SmtpMail.SmtpServer = mailSettings.Smtp.Network.Host;//"137.132.244.140";
            try
            {
                SmtpMail.Send(msgMail);
            }
            catch (Exception ex)
            {
                //email cannot send
            }

            MultiView1.ActiveViewIndex = 1;
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            firstname.Text = "";
            lastname.Text = "";
            if (!User.Identity.IsAuthenticated)
            {
                email.Text = "";
            }
            enquiry.Text = "";
            MultiView1.ActiveViewIndex = 0;
        }
    }
}
